package com.eascs.cloudFile.dao;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.recordlog.constant.O2oLogType;
import com.eascs.cloudFile.entity.CloudDatalist;
import com.eascs.cloudFile.vo.SaveFileVo;
import com.eascs.datalist.dao.O2oDataListDao;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.file.FileTypeConstants;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

/**
 * o2o-service库DAO
 * 
 * @author user
 *
 */
@Repository
public class FilesDao extends BaseO2oDao {

	@Autowired
	private O2oDataListDao o2oDataListDao;

	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;

	public String saveFile(CloudDatalist cloudDatalist, SaveFileVo saveFileVo) {
		O2oDataListEntity result = new O2oDataListEntity();
		String datalistCode = saveFileVo.getDataListType();
		String loanapplyno = saveFileVo.getLoanapplyNo();
		User user = UserHolder.getUser();
		List<O2oDataListEntity> list = o2oDataListDao.findDateEntity(loanapplyno, datalistCode);
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyno);
		if (list != null && list.size() > 0) {
			if (datalistCode.equals(FileTypeConstants.DALIST_TYPE_OTH)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_BZJDATA)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_CONTRACT)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_BC_CONTRACT)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_GUIDELINE)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_NSFJ)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_JQ_NSFJ)
					|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_PICTURE)
					|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_EVIDENCE)
					|| datalistCode.equals(FileTypeConstants.BRAND_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.LOAN_CHECK_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_TQHK)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_DCWD)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_CREDITFILING)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_O2OPDR)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_FKZZWJ)
					|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_PICTURE)
					|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_EVIDENCE)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_DK_PZ)
					) {
				if (datalistCode.equals(FileTypeConstants.DALIST_TYPE_OTH)) {
					if (o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NEW)
							|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_ZX_BEGIN)) {
						datalistCode=FileTypeConstants.DALIST_TYPE_ZXFJ;
					}else if(o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_ZX_END)
							|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NS_BEGIN)){
						datalistCode=FileTypeConstants.DALIST_TYPE_IAFJ;
					}else if(o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NS_END)
							|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_SB_BEGIN)){
						datalistCode=FileTypeConstants.DALIST_TYPE_MQFJ;
					}else{
						datalistCode=FileTypeConstants.DALIST_TYPE_AFTER_LOAN_ADD;
					}
				}
				O2oDataListEntity loanEntity = new O2oDataListEntity();
				Date date = new Date();
				loanEntity.setCreatDate(date);
				loanEntity.setUpdateTime(date);
				loanEntity.setUrlpath(cloudDatalist.getUrlPath());
				loanEntity.setOriginalName(cloudDatalist.getOriginalName());
				loanEntity.setDatalistCode(datalistCode);
				loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
				loanEntity.setCreatId(user.getNo());
				loanEntity.setCreatOr(user.getCname());
				loanEntity.setExtendNmae(cloudDatalist.getExtendName());
				loanEntity.setLoanapplyNo(loanapplyno);
				loanEntity.setIsDelete(StatusContexts.O2O_NO);
				this.insert(loanEntity);
				result = loanEntity;
				insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + loanEntity.getDatalistNo());
			} else {
				for (int i = 0; i < list.size(); i++) {
					Date date = new Date();
					O2oDataListEntity loanEntity = list.get(i);
					loanEntity.setIsDelete(StatusContexts.O2O_YES);
					loanEntity.setUpdateTime(date);
					this.updateById(loanEntity);// 先隐藏=删除在新增一条数据
					insertLog(O2oLogType.FILE_STATUS_N, "更新，上传其他附件,ID:" + loanEntity.getDatalistNo());
					O2oDataListEntity dateList = new O2oDataListEntity();
					dateList.setUrlpath(cloudDatalist.getUrlPath());
					dateList.setOriginalName(cloudDatalist.getOriginalName());
					dateList.setDatalistCode(datalistCode);
					dateList.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
					dateList.setCreatId(user.getNo());
					dateList.setCreatOr(user.getCname());
					dateList.setCreatDate(date);
					dateList.setUpdateTime(date);
					dateList.setExtendNmae(cloudDatalist.getExtendName());
					dateList.setLoanapplyNo(loanapplyno);
					dateList.setIsDelete(StatusContexts.O2O_NO);
					this.insert(dateList);
					result = dateList;
					insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + dateList.getDatalistNo());
				}
			}
		} else {
			if (datalistCode.equals(FileTypeConstants.DALIST_TYPE_OTH)) {
				if (o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NEW)
						|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_ZX_BEGIN)) {
					datalistCode=FileTypeConstants.DALIST_TYPE_ZXFJ;
				}else if(o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_ZX_END)
						|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NS_BEGIN)){
					datalistCode=FileTypeConstants.DALIST_TYPE_IAFJ;
				}else if(o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NS_END)
						|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_SB_BEGIN)){
					datalistCode=FileTypeConstants.DALIST_TYPE_MQFJ;
				}else{
					datalistCode=FileTypeConstants.DALIST_TYPE_AFTER_LOAN_ADD;
				}
			}
			O2oDataListEntity loanEntity = new O2oDataListEntity();
			Date date = new Date();
			loanEntity.setCreatDate(date);
			loanEntity.setUpdateTime(date);
			loanEntity.setUrlpath(cloudDatalist.getUrlPath());
			loanEntity.setOriginalName(cloudDatalist.getOriginalName());
			loanEntity.setDatalistCode(datalistCode);
			loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
			loanEntity.setCreatId(user.getNo());
			loanEntity.setCreatOr(user.getCname());
			loanEntity.setExtendNmae(cloudDatalist.getExtendName());
			loanEntity.setLoanapplyNo(loanapplyno);
			loanEntity.setIsDelete(StatusContexts.O2O_NO);
			this.insert(loanEntity);
			result = loanEntity;
			insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + loanEntity.getDatalistNo());
		}
		String url = result.getDatalistNo() + "." + result.getExtendNmae();
		return url;
	}

	/**
	 * @Description:
	 *               <p>
	 *               插入文件修改日志
	 *               </p>
	 * @author CXW
	 * @date 2016年9月26日 下午2:35:13
	 * @version V1.0
	 * @param logType
	 *            日志类型,updateStr 修改内容
	 * @return void
	 */
	public void insertLog(String logType, String updateStr) {
		try {
			o2oLoanapplyService.saveLog(UserHolder.getUser().getCname(), logType, null, updateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
